<template>
  <div>
    <!-- 上牌信息审核 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjRegistrationAudit"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 1)" :disabled="isOpen">审批通过</multiuse-button>
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 2)" :disabled="isOpen">审批驳回</multiuse-button>
      </template>
    </FilterTables>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownerClose" />
    </BaseDialog>
    <Drawer :visible.sync="visible" :title="'详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="85%">
      <childrenModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isreject" :title="'驳回意见'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <reject :list="selectedList" @close="rejectclose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, updatePlateApply, vehicleConfiguration, getDeptList, getOfficeList } from '@/api/omSale/Registration/RegistrationAudit';
import { dictionary } from '@/utils/dictMixin';
import ownerSeacher from './ownerSeacher';
import childrenModel from './childrenModel';
import reject from './reject';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        checkStatus: 60191002,
        xhrAbort: true
      },
      formData: {},
      visible: false,
      issearch: false,
      isreject: false,
      isOpen: true,
      selectedList: []
    };
  },
  components: {
    ownerSeacher,
    childrenModel,
    reject
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getDeptList();
    this.getList();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'RANGE_DATE',
          label: '首次提报日期',
          fieldName: 'startReportDate|endReportDate',
          placeholder: '请选择',
          initialValue: []
        },

        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装'] },
          itemList: []
        },
        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '请选择',
          itemList: [],
          change: p => {
            if (p) {
              this.topFilterList.map(x => {
                if (x.fieldName == 'officeId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
              this.getOfficeList(p);
            } else {
              this.topFilterList.map(x => {
                if (x.fieldName == 'officeId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '办事处',
          fieldName: 'officeId',
          placeholder: '请选择',
          itemList: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '上牌审批状态',
          dataIndex: 'checkStatus',
          sorter: true,
          render: p => {
            return <span>{this.createDictText(p.row.checkStatus, '6019')}</span>;
          }
        },
        {
          title: '零售单号',
          dataIndex: 'retailOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户编码',
          dataIndex: 'potentialCustomerNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '性别',
          dataIndex: 'customerSex',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1002'),
          render: p => {
            return <span>{this.createDictText(p.row.customerSex, '1002')}</span>;
          }
        },
        {
          title: '手机号',
          dataIndex: 'mobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电话',
          dataIndex: 'phone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '使用人',
          dataIndex: 'userName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '使用人电话',
          dataIndex: 'userPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '身份证号',
          dataIndex: 'cardNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票号',
          dataIndex: 'invoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票价格',
          dataIndex: 'invoiceAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票附件',
          dataIndex: 'fpfj',
          sorter: true,
          render: p => {
            let list = p.row.enclosures || [];
            let array = [];
            if (list.length > 0) {
              let arr = [];
              for (let i = 0; i < list.length; i++) {
                if (list[i].enclosureType == 'invoiceAddress') {
                  array.push({ text: list[i].enclosureName, value: list[i].enclosureUrl });
                }
              }
            }
            return (
              <div>
                {array.map(v => {
                  return (
                    <div>
                      <a style="color:#bb0a30" href={v.value} target="_blank" download>
                        {v.text}
                      </a>
                    </div>
                  );
                })}
              </div>
            );
          }
        },
        {
          title: '行驶证附件',
          dataIndex: 'xszfj',
          sorter: true,
          render: p => {
            let list = p.row.enclosures || [];
            let array = [];
            if (list.length > 0) {
              let arr = [];
              for (let i = 0; i < list.length; i++) {
                if (list[i].enclosureType == 'drivingAddress') {
                  array.push({ text: list[i].enclosureName, value: list[i].enclosureUrl });
                }
              }
            }
            return (
              <div>
                {array.map(v => {
                  return (
                    <div>
                      <a style="color:#bb0a30" href={v.value} target="_blank" download>
                        {v.text}
                      </a>
                    </div>
                  );
                })}
              </div>
            );
          }
        },
        {
          title: '车牌号',
          dataIndex: 'plateNumber',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '行驶证注册日期',
          dataIndex: 'drivingRegisteDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '颜色',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌省份',
          dataIndex: 'plateProvinceName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌城市',
          dataIndex: 'plateCityName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '首次实销提报日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '实销通过日期',
          dataIndex: 'reportUpdatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '首次上牌提报日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '出厂日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '入库日期',
          dataIndex: 'postingDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '交车日期',
          dataIndex: 'deliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '市场指导价',
          dataIndex: 'buyPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机',
          dataIndex: 'engineNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '下线工厂',
          dataIndex: 'factoryName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let isOpen = true;
        let is = false;
        row.map(x => {
          if (x.checkStatus == 60191002) {
            isOpen = false;
          } else {
            is = true;
          }
        });
        this.isOpen = !isOpen && !is ? false : true;
        this.selectedList = row;
      } else {
        this.selectedList = [];
        this.isOpen = true;
      }
    },
    seacherFun() {
      this.issearch = true;
    },
    async addHandle(type) {
      if (type == 1) {
        await this.$confirm('是否通过?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectedList.map(x => {
              arr.push(x.applyId);
            });
            let res = await updatePlateApply({ checkStatus: 60191003, ids: arr });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '通过成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else {
        this.isreject = true;
      }
    },
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    async getDeptList() {
      let res = await getDeptList({ orgType: 15061005 });
      let arr = res.data.map(x => ({ text: x.orgName, value: x.orgId }));
      this.topFilterList.find(x => x.fieldName == 'deptId').itemList = arr;
    },
    async getOfficeList(orgid) {
      let res = await getOfficeList({ orgType: 15061007, parentOrgId: orgid });
      let arr = res.data.map(x => ({ text: x.orgName, value: x.orgId }));
      this.topFilterList.find(x => x.fieldName == 'officeId').itemList = arr;
      this.topFilterList.find(x => x.fieldName == 'officeId').initialValue = '';
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.visible = true;
    },
    ownerClose(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    emitcloseHandler() {
      this.visible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    rejectclose() {
      this.isreject = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
